package com.ruoyi.web.controller.homepage;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.homepage.domain.TbTag;
import com.ruoyi.homepage.service.ITbTagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TB_TAG测试Controller
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/test/tbtag")
public class TbTagTestController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(TbTagTestController.class);

    @Autowired
    private ITbTagService tbTagService;

    /**
     * 测试连接
     */
    @GetMapping("/ping")
    public AjaxResult ping()
    {
        return AjaxResult.success("TB_TAG服务连接正常");
    }

    /**
     * 测试查询列表
     */
    @GetMapping("/list")
    public AjaxResult testList()
    {
        try {
            TbTag tbTag = new TbTag();
            List<TbTag> list = tbTagService.selectTbTagList(tbTag);
            return AjaxResult.success("查询成功", list);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 测试新增
     */
    @GetMapping("/add")
    public AjaxResult testAdd()
    {
        try {
            TbTag tbTag = new TbTag();
            tbTag.setTagName("测试标签");
            tbTag.setTagType(1);
            tbTag.setTagStatus(1);
            int result = tbTagService.insertTbTag(tbTag);
            return AjaxResult.success("新增成功", result);
        } catch (Exception e) {
            logger.error("新增标签失败", e);
            return AjaxResult.error("新增失败: " + e.getMessage());
        }
    }

    /**
     * 诊断系统状态
     */
    @GetMapping("/diagnose")
    public AjaxResult diagnose()
    {
        Map<String, Object> result = new HashMap<>();

        try {
            // 检查Service是否注入
            result.put("serviceInjected", tbTagService != null);

            // 检查数据库连接
            if (tbTagService != null) {
                TbTag tbTag = new TbTag();
                List<TbTag> list = tbTagService.selectTbTagList(tbTag);
                result.put("databaseConnected", true);
                result.put("recordCount", list.size());
                result.put("records", list.size() > 0 ? list.subList(0, Math.min(3, list.size())) : new ArrayList<>());
            } else {
                result.put("databaseConnected", false);
                result.put("error", "Service未正确注入");
            }

            return AjaxResult.success("诊断完成", result);
        } catch (Exception e) {
            logger.error("诊断失败", e);
            result.put("error", e.getMessage());
            result.put("errorType", e.getClass().getSimpleName());
            return AjaxResult.error("诊断失败", result);
        }
    }
}
